/**
 * 脱敏手机号码
 *
 * @param content 待脱敏的手机号内容
 * @param fillChar 脱敏填充字符，默认为 "*"
 * @returns 脱敏后的手机号字符串
 */
function des_phone(content, fillChar = "*") {
  if (!content) {
    return "";
  }

  // 非字符串转换为字符串
  content = content.toString();

  if (content.length < 11) {
    return content;
  }

  let index = 1;
  let result = "";

  for (let char of content) {
    if (index < 4 || index > content.length - 4) {
      result += char;
    } else {
      result += fillChar;
    }
    index++;
  }
  return result;
}

export { des_phone };

/**
 * 脱敏处理字符串中的名字
 *
 * @param content 待脱敏的字符串
 * @param fillChar 用于脱敏的填充字符，默认为 "*"
 * @returns 脱敏后的字符串
 */
function des_name(content, fillChar = "*") {
  if (!content) {
    return "";
  }

  let index = 1;
  let result = "";

  for (let char of content) {
    if (index === 1 || (index === content.length && index !== 2)) {
      result += char;
    } else {
      result += fillChar;
    }
    index++;
  }
  return result;
}

export { des_name };

/**
 * 对证件号码进行脱敏处理
 *
 * @param content 待脱敏的内容
 * @param fillChar 脱敏时用于填充的字符，默认为 "*"
 * @returns 脱敏后的字符串
 */
function des_ID(content, fillChar = "*") {
  if (!content) {
    return "";
  }

  // 非字符串转换为字符串
  content = content.toString();

  if (content.length < 9) {
    return content;
  }

  let index = 1;
  let result = "";

  for (let char of content) {
    if (index < 5 || index > content.length - 4) {
      result += char;
    } else {
      result += fillChar;
    }
    index++;
  }
  return result;
}

export { des_ID };
